package com.zzyl.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.dto.UserLoginRequestDto;
import com.zzyl.entity.Member;
import com.zzyl.mapper.MemberMapper;
import com.zzyl.properties.JwtTokenConfigProperties;
import com.zzyl.service.MemberService;
import com.zzyl.service.WechatService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private WechatService wechatService;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private JwtTokenConfigProperties jwtTokenConfigProperties;

    @Override
    public LoginVo login(UserLoginRequestDto userLoginRequestDto) {
        //1. appid + appsecret + code 调取第三方微信官方去获取openid
//        String openId = wechatService.getOpenId(userLoginRequestDto.getCode());

        //2.通过openid 去查询数据库是否之前存在该用户进行登录
//        Member member = memberMapper.getMemberByopenId(openId);
        //2.1 不存在 ---新增用户
//        if (ObjectUtil.isEmpty(member)){
//            member = new Member();
//            member.setOpenId(openId);
//        }
        //2.2 存在 ---不新增
        //2.2.1 -----根据phoneCode 去微信官方去查询手机号
        /**
         * 获取电话号码，先获取access_token,然后在调电话号码
         */
//        String phoneNumber = wechatService.getPhone(userLoginRequestDto.getPhoneCode());
        //2.3 存在 ---判断手机号是否一致，不一致就更新手机号
//        if (!member.getPhone().equals(phoneNumber)){
//            member.setPhone(phoneNumber);
//        }
        //先完成更新
//        if (ObjectUtil.isNotEmpty(member.getId())){
//            memberMapper.updateMemberByid(member);
//        }else {
//            member = new Member();
//            member.setOpenId(openId);
//            member.setPhone(phoneNumber);
//            member.setName("孔德强");
//            memberMapper.insert(member);
//        }


        //3. 获取用户id，jwt生成token
//        return null;


        //1. appid + appsecret + code 调取第三方微信官方去获取openid
        String openId = wechatService.getOpenId(userLoginRequestDto.getCode());
        String phoneNumber = wechatService.getPhone(userLoginRequestDto.getPhoneCode());
        //2.通过openid 去查询数据库是否之前存在该用户进行登录
        Member member = memberMapper.getMemberByopenId(openId);
        //2.1 不存在 ---新增用户
        if (ObjectUtil.isNotEmpty(member)) { //member不为空
            //电话号码被更改
            if (!ObjectUtil.equals(member.getPhone(),phoneNumber)){
                member.setPhone(phoneNumber);
                memberMapper.updateMemberByid(member);
            }
        }else{ //member为空 ，就去插入
            member = new Member();
            member.setOpenId(openId);
            member.setPhone(phoneNumber);
            member.setName("孔德强");
            memberMapper.insert(member);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId",member.getId());
        String token = JwtUtil.createJWT(jwtTokenConfigProperties.base64EncodedSecretKey, jwtTokenConfigProperties.getTtl(), map);
        //3.获取用户id,jwt 生成token
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(member.getName());
        return loginVo;
    }
}

















